//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package dao;

import java.sql.*;

public final class DBHelper {
    static String _connStr = "jdbc:sqlserver://139.155.250.85:1433;DatabaseName=elm;TrustServerCertificate=true;user=elmUser;password=elmuser;";

    public DBHelper ( ) {
    }

    public static String getConnectionString ( ) {
        return _connStr;
    }

    public static void setConnectionString ( String connStr ) {
        _connStr = connStr;
    }

    public static Connection getConnection ( ) throws SQLException {
        String conStr = getConnectionString ();
        return getConnection ( conStr );
    }

    public static Connection getConnection ( String connStr ) throws SQLException {
        Connection con = DriverManager.getConnection ( connStr );
        return con;
    }

    public static int getMaxValue ( String db , String row ) throws SQLException {
        String sql = "select max(" + row + ") from " + db;
        Connection con = getConnection ();
        Statement stm = con.createStatement ();
        ResultSet result = stm.executeQuery ( sql );
        int maxValue = 0;
        if (result.next ()) {
            maxValue = Integer.parseInt ( result.getString ( 1 ).trim () );
        }
        return maxValue;
    }

    public static int getRecordNum ( String db ) throws SQLException {
//        select count(*) from food;
        String sql = "select count(*) from " + db;
        Connection con = getConnection ();
        Statement stm = con.createStatement ();
        ResultSet result = stm.executeQuery ( sql );
        int rowCount = 0;
        if (result.next ()) {
            rowCount = result.getInt ( 1 );
        }
        return rowCount;
    }

    public static ResultSet executeQuery ( String sql ) throws SQLException {
        Connection con = getConnection ();
        Statement stm = con.createStatement ();
        ResultSet result = stm.executeQuery ( sql );
        return result;
    }

    public static int executeUpdate ( String sql ) throws SQLException {
        Connection con = getConnection ();
        Statement stm = con.createStatement ();
        int result = stm.executeUpdate ( sql );
        return result;
    }

    public static void close ( ) throws SQLException {
        Connection con = getConnection ();
        con.close ();
    }
}
